package com.xjm.controller.superadmin;

import com.xjm.model.bo.HeadLine;
import com.xjm.model.dto.Result;
import com.xjm.model.vo.Page;
import com.xjm.service.solo.HeadLineService;
import org.myspringframework.core.annotation.Controller;
import org.myspringframework.core.inject.annotation.Autowired;
import org.myspringframework.mvc.annotation.RequestMapping;
import org.myspringframework.mvc.annotation.RequestParam;
import org.myspringframework.mvc.annotation.ResponseBody;
import org.myspringframework.mvc.type.ModelAndView;
import org.myspringframework.mvc.type.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author jaymin
 * 2020/11/7 22:46
 */
@Controller
@RequestMapping("/headline")
public class HeadLineOperationController {
    @Autowired
    private HeadLineService headLineService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ModelAndView addHeadline(@RequestParam("lineName") String lineName,
                                    @RequestParam("lineLink") String lineLink,
                                    @RequestParam("lineImg") String lineImg,
                                    @RequestParam("priority") Integer priority) {
        HeadLine headLine = HeadLine.builder().lineName(lineName)
                .lineLink(lineLink)
                .lineImg(lineImg)
                .priority(priority)
                .build();
        ModelAndView headline = new ModelAndView().setView("hello.jsp").addViewData("result", headLine);
        return headline;
    }

    /**
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Result<Boolean> deleteHeadline(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") Integer id) {
        // TODO: 参数校验与请求参数转换
        return headLineService.deleteHeadline(id);
    }

    /**
     * @param request
     * @param response
     * @return
     */
    public Result<Boolean> updateHeadline(HttpServletRequest request, HttpServletResponse response) {
        // TODO: 参数校验与请求参数转换
        return headLineService.updateHeadline(new HeadLine());
    }

    /**
     * 根据主键查询头条
     *
     * @param request
     * @param response
     * @return
     */
    public Result<HeadLine> getHeadlineById(HttpServletRequest request, HttpServletResponse response) {
        // TODO: 参数校验与请求参数转换
        return headLineService.getHeadlineById(0);
    }

    /**
     * 获取头条列表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "query", method = RequestMethod.GET)
    @ResponseBody
    public Result<List<HeadLine>> getHeadlineList(HttpServletRequest request, HttpServletResponse response) {
        HeadLine headLine = new HeadLine();
        return headLineService.getHeadlineList(headLine, new Page());
    }
}
